<?php

namespace Gql\Middleware;

use Gql\Contracts\MiddlewareInterface;
use Gql\Core\Context;
use Gql\Core\ParseInfo;
use Illuminate\Contracts\Support\Arrayable;

/**
 * 解析ResolveInfo数据生成用户请求的一些字段需求等
 * Class GraphInitialMiddleware
 * @package App\PhpGraph\Middleware
 */
class InitialMiddleware implements MiddlewareInterface
{

    public function handle(Context $ctx, callable $next)
    {
        $parse=new ParseInfo($ctx,$ctx->info);
        $ctx=$parse->parse();
        $response = $next($ctx);
        $response = $response instanceof Context ? $response->body : $response;
        return $response instanceof Arrayable ? $response->toArray() : $response;
    }
}